package com.family.web.controller.pedigree;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.family.common.core.controller.BaseController;
import com.family.common.core.domain.AjaxResult;
import com.family.common.core.page.TableDataInfo;
import com.family.common.utils.StringUtils;
import com.family.pedigree.domain.FamilyUserInsurance;
import com.family.pedigree.service.IFamilyUserInsuranceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 个人保修信息表 前端控制器
 * </p>
 *
 * @author forever
 * @since 2025-01-16
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/family/user/insurance")
public class FamilyUserInsuranceController extends BaseController {

    private final IFamilyUserInsuranceService familyUserInsuranceService;

    @PostMapping
    public AjaxResult add(@Validated @RequestBody FamilyUserInsurance familyUserInsurance) {
        familyUserInsuranceService.save(familyUserInsurance);
        return AjaxResult.success();
    }

    @PutMapping
    public AjaxResult edit(@Validated @RequestBody FamilyUserInsurance familyUserInsurance) {
        familyUserInsuranceService.updateById(familyUserInsurance);
        return AjaxResult.success();
    }

    @DeleteMapping("/{insuranceId}")
    public AjaxResult delById(@PathVariable Long insuranceId) {
        familyUserInsuranceService.removeById(insuranceId);
        return AjaxResult.success();
    }

    @GetMapping("/page")
    public TableDataInfo page(FamilyUserInsurance familyUserInsurance) {
        startPage();
        List<FamilyUserInsurance> result = familyUserInsuranceService.list(Wrappers.<FamilyUserInsurance>lambdaQuery()
                .eq(Objects.nonNull(familyUserInsurance.getInfoId()), FamilyUserInsurance::getInfoId, familyUserInsurance.getInfoId())
                .like(StringUtils.isNotBlank(familyUserInsurance.getInsuranceType()), FamilyUserInsurance::getInsuranceType, familyUserInsurance.getInsuranceType())
                .eq(Objects.nonNull(familyUserInsurance.getParticularYear()), FamilyUserInsurance::getParticularYear, familyUserInsurance.getParticularYear())
                .orderByAsc(FamilyUserInsurance::getParticularYear));
        return getDataTable(result);
    }

}
